<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * home.php
 * @author ranuandra
 *
 * 8 Mar 2012 11:30:41
 *
 * 
 */

class Controller_Home Extends Kohana_Nasmoco
{
	public function action_index()
	{
		$category = ORM::factory('category')->where('category', '=', 'Home')->find();
		$datas = ORM::factory('mstlowongan')->where('DATE("publisheddate")', '<=', date('Y-m-d'))->where('DATE("dateto")', '>=', date('Y-m-d'))->find_all();
		//print_r($datas);
		//$this->banner = View::factory('module/banner');
		//$this->right = View::factory('right/login');
		//$tahapan = View::factory('right/tahapan');
		//$this->header = View::factory('module/header');
		//$this->right->captcha = Captcha::instance();
		//$this->right->datas = $datas;
		//$this->header->banner = $this->banner;
		//$this->header->right = $this->right;
		$view = View::factory('home/index');
		
		$quote = ORM::factory('mstquote')->limit(1)->order_by(DB::expr('RAND()'))->find();
		//print_r($quote);
		$view->datas = $datas;
		$view->quote = $quote;
		$view->homes = $category->content->find_all();
		
		$this->template->container = $view;

	}

	
	public function action_register()
	{
		$form = array(
						'username' 			=> '',
						'email'	   			=> '',
						'nama_lengkap'		=> '',
						'tmp_lahir'			=> '',
						'tanggal'			=> '',
						'bulan'				=> '',
						'tahun'				=> '',
						'jenis_kelamin'		=> '',
		                'mstagama_id'		=> '',
		                'phone'				=> '',
						'mobilephone'		=> '',
		  				'no_ktp'			=> '',
						'alamat_ktp'		=> '',
						'rt_ktp'			=> '',
						'kelurahan_ktp'		=> '',
						'kecamatan_ktp'		=> '',
						'kodepos_ktp'		=> '',
						'kota_ktp'			=> '',
						'propinsi_ktp'		=> '',
						'alamat'			=> '',
						'rt'				=> '',
						'kelurahan'			=> '',
						'kecamatan'			=> '',
						'kodepos'			=> '',
						'kota'				=> '',
						'propinsi'			=> '',
						'mstpendidikan_id'	=> '',
						'mstjurusan_id'		=> '',
						'institusi'			=> '',
						'levelpengalaman'	=> '',
						'term'				=> '',
						'tinggi'			=> '',
						'berat'				=> '',
						'golongan_darah'	=> '',
						'status_nikah'		=> '',
						'flag_organisasi'	=> '',
						'flag_ketrampilan'	=> '',
						'no_npwp'			=> '',
						'no_jamsostek'		=> '',
						'country_id'		=> '',
						'ipk'				=> '',
						'thn_lulus'			=> '',
						'jml_anak'			=> '',
						 
							
		);
		$countries = ORM::factory('country')->find_all()->as_array('id', 'country_name');
		$agamas = ORM::factory('mstagama')->find_all()->as_array('id', 'agama');
		$jurusan = ORM::factory('mstjurusan')->find_all()->as_array('id', 'jurusan');
		$pendidikan = ORM::factory('mstpendidikan')->find_all()->as_array('id', 'pendidikan');
		$view = View::factory('home/register');
		$view->right = View::factory('register/right');
		$view->agamalist = $agamas;
		//$view->jurusanlist = $jurusan;
		$view->jurusanlist = array('Silahkan Pilih');
		$view->pendidikanlist = $pendidikan;
		$view->form = $form;
		$view->captcha = Captcha::instance();
		$view->countries = $countries;
		$this->template->container = $view;
	}
	
	public function action_save()
	{
		if(isset($_POST))
		{
			if(Captcha::valid(Request::current()->post('captcha')))
			{
				$db = Database::instance();
				$db->begin();
				
				try {
						$pin = $this->_createpin();
						$userform = array(
										'username' 			=> Request::current()->post('email'),
										'password' 			=> Request::current()->post('password'),
										'password_confirm'	=> Request::current()->post('password_confirm'),
										'email'				=> Request::current()->post('email'),
									);
						
						$user = ORM::factory('user');
						$user->pin = $pin;
                        $user->activated = 1;
						$user->create_user($userform, array(
					 										'username',
															'password',
					 										'email',
					  									));
		
					  	$login_role = new Model_Role(array('name' =>'login'));
					  	$user_role = new Model_Role(array('name' =>'user'));
						$user->add('roles',$login_role);
						$user->add('roles',$user_role);
						
						$pelamar = ORM::factory('mstpelamar');
						$pelamar->tgl_lahir = $_POST['tahun']."-".$_POST['bulan']."-".$_POST['tanggal'];
						$pelamar->user_id = $user->id;
						$pelamar->created = date("Y-m-d H:i:s");
						if(Request::current()->post('alamatsama') == 1)
						{
							$_POST['alamat'] = Request::current()->post('alamat_ktp');
							$_POST['rt'] = Request::current()->post('rt_ktp');
							$_POST['kelurahan'] = Request::current()->post('kelurahan_ktp');
							$_POST['kecamatan'] = Request::current()->post('kecamatan_ktp');
							$_POST['kota'] = Request::current()->post('kota_ktp');
							$_POST['kodepos'] = Request::current()->post('kodepos_ktp');
							$_POST['propinsi'] = Request::current()->post('propinsi_ktp');
						}
						$pelamar->create_data($_POST);
						
						
						$pendidikan = ORM::factory('pelamarpendidikan');
						$pendidikan->mstpelamar_id = $pelamar->id;
						$pendidikan->created = date("Y-m-d H:i:s");

						
						$pendidikan->create_data($_POST);
						
						$db->commit();
						
						$msgContent = "Kode Aktifasi anda : ". $pin;
						/*$mailer = Email::connect();
			 			$message = Swift_Message::newInstance();
			
			        	$message->setSubject('Kode Aktivasi Nasmoco.com')
			              		->setFrom(array('hrd@nasmoco.com' => 'nasmoco'))
			              		->setTo(array($_POST['email'] => ''))
			              		->setBody($msgContent)
								->setContentType("text/html");
			            $mailer->send($message);*/
						/*$subject = "Konfirmasi Registrasi Nasmoco";
						$message = "Kode Aktifasi anda : http://hrd.nasmoco.com/aktivasi?email=".$_POST['email']."&pin=".$pin;
						$from = "hrd@nasmoco.com";*/
						
						
						
			            /*$mail = Email::factory($subject, $message);
			            $mail->from($from);
			            $mail->to($_POST['email']);
			            $mail->send();*/
						
						
						 //edit upload 11-05-2012
						$to = $_POST['email'];
						$subject = "Registrasi Nasmoco";
						$message = "Kode Aktifasi anda : http://karir.nasmoco.com/aktivasi?email=".$_POST['email']."&pin=".$pin;
						$from = "hrd@nasmoco.com";
						$headers = "From:" . $from;
						//mail($to,$subject,$message,$headers);
						
						$message = '<div class="alert alert-success">';
						$message .= 'Anda telah berhasil melakukan pendaftaran, untuk langkah selanjutnya silahkan anda buka E-mail anda dan segera aktifkan dengan meng klik link yang telah kami kirimkan ke email anda';
						$message .= '</div>';
			            $view = View::factory('home/sukses');
			            //$view->message = "Pendaftaran berhasil, pin : ".$pin;
			            $view->message = $message;
			            $this->template->container = $view;
						
						//return Request::current()->redirect(URL::base()."home/");
						
				} 
				catch (ORM_Validation_Exception $e) 
				{
					$db->rollback();
					$errors = $e->errors('models');
					print_r($errors);
					$countries = ORM::factory('country')->find_all()->as_array('id', 'country_name');
					$agamas = ORM::factory('mstagama')->find_all()->as_array('id', 'agama');
					$jurusan = ORM::factory('mstjurusan')->find_all()->as_array('id', 'jurusan');
					$pendidikan = ORM::factory('mstpendidikan')->find_all()->as_array('id', 'pendidikan');
					$view = View::factory('home/register');
					$view->right = View::factory('register/right');
					$view->agamalist = $agamas;
					//$view->jurusanlist = $jurusan;
					$view->jurusanlist = array('Silahkan Pilih');
					$view->pendidikanlist = $pendidikan;
					$view->form = $_POST;
					$view->captcha = Captcha::instance();
					$view->countries = $countries;
					$this->template->container = $view;
				}
			}
			else 
			{
				$errors['captcha'] = "Kode yang anda masukan tidak sesuai";
				$countries = ORM::factory('country')->find_all()->as_array('id', 'country_name');
				$agamas = ORM::factory('mstagama')->find_all()->as_array('id', 'agama');
				$jurusan = ORM::factory('mstjurusan')->find_all()->as_array('id', 'jurusan');
				$pendidikan = ORM::factory('mstpendidikan')->find_all()->as_array('id', 'pendidikan');
				$view = View::factory('home/register');
				$view->right = View::factory('register/right');
				$view->agamalist = $agamas;
				//$view->jurusanlist = $jurusan;
				$view->jurusanlist = array('Silahkan Pilih');
				$view->pendidikanlist = $pendidikan;
				$view->form = $_POST;
				$view->captcha = Captcha::instance();
				$view->countries = $countries;
				$this->template->container = $view;			
			}
		}
	}

	protected function _createpin()
	{
		while (TRUE) {
			$code = text::random('alnum', 10);
			$count = DB::select('id')
				->where('pin', '=', $code)
				->from('users')
				->execute()
				->count();
			if ($count === 0){
				return $code;
			}
		}
	}
	
	public function action_login()
	{
		    $view = View::factory('home/login');
		    $view->captcha = Captcha::instance();
		    $this->template->container = $view;
	
	}
	
	public function action_profile()
	{
		$this->template->container = "<h1>Profile Nasmoco</p>";
	}
	
	public function action_kontak()
	{
		$this->template->container = "<h1>Kontak Kami</p>";
	}
	
	public function action_jurusan()
	{
		$id = Request::current()->param('id');
		$jurusan = ORM::factory('mstjurusan')->where('mstpendidikan_id', '=', $id)->find_all()->as_array('id', 'jurusan');
		$view = View::factory('home/jurusan');
		$view->jurusanlist = $jurusan;
		
		if(Request::current()->is_ajax() == $this->request)
		{
			echo $view;
		}
		else 
		{
			$this->template->container = $view;
		}
	}
}